package com.ylzinfo.ms.fetchdemo;

import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Product: IntelliJ IDEA
 * @Author Rogchen rogchen128@gmail.com
 * @Created Date: 20-2-12 16:57
 **/
public class 火狐se {


    public static void main(String[] args) {
        String url ="https://baidu.com";
//        String url = "https://ssc.mohrss.gov.cn/main?_api_timestamp=1580909751215&security=VxYGjhhH1yTAm7OgefOywfWQ27nVlhiV8t8a3fQdWgSwh6xcJw1BfT0aE%2BjyXQXuiENOjW%2Bgp89zicigqxzUlTvwh44xeQSPzwNT8poORIbzJFl4cTRv7wjMaZtiH60WAYgzHE7m1R%2Foo6AMnhwTYva0yNa4HkRXGsd6FglBINgEHsAsotPDd0d2diVfRBjBYRgOTEQzJVBLcMMIHqMhYA%3D%3D&_api_name=get_token&api_access_key=15dfa422d02d49108741f011b6a5e3f9&_api_signature=iteFyLXHfdIzy6639R%2FmZ%2FsQHfc%3D&return_url=http%3A%2F%2Fwww.ylzinfo.com&_api_version=1.0.0&_api_access_key=15dfa422d02d49108741f011b6a5e3f9";
        System.setProperty("webdriver.gecko.driver","/media/chenhk/working/projects/maven/fetch-demo/src/test/java/com/ylzinfo/ms/fetchdemo/geckodriver");
        WebDriver driver = new FirefoxDriver();
        //驱动的网址
        driver.get(url);
        try
        {
//            Thread.sleep(60000);// 60秒
            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
//        //浏览器窗口变大
        driver.manage().window().maximize();
//        //定位输入框元素
        WebElement txtbox = driver.findElement(By.name("wd"));
//
//        //在输入框输入文本
        txtbox.sendKeys("HelloWorld");
//
//        //定位按钮元素
        WebElement btn = driver.findElement(By.id("su"));
//
//        //点击按钮
        btn.click();
//
//        Document document = Jsoup.parse(driver.getPageSource());
//        System.out.println(document.body());
        //关闭驱动
        driver.close();
    }

    @Test
    public void 测试2(){
        System.setProperty("webdriver.gecko.driver","/media/chenhk/working/projects/maven/fetch-demo/src/test/java/com/ylzinfo/ms/fetchdemo/geckodriver");
        WebDriver driver = new FirefoxDriver();
        String url = "http://www.baidu.com";
        driver.get(url); // 打开指定的网站

        //driver.navigate().to(url); // 打开指定的网站

        /*
         *
         * driver.findElement(By.id("kw")).sendKeys(new String[] { "hello" });//
         * 找到kw元素的id，然后输入hello driver.findElement(By.id("su")).click(); // 点击按扭
         */
        try {
            /**
             * WebDriver自带了一个智能等待的方法。 dr.manage().timeouts().implicitlyWait(arg0, arg1）；
             * Arg0：等待的时间长度，int 类型 ； Arg1：等待时间的单位 TimeUnit.SECONDS 一般用秒作为单位。
             */
            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //获取当前浏览器的信息
        System.out.println("Title:" + driver.getTitle());
        System.out.println("currentUrl:" + driver.getCurrentUrl());

        //执行js脚本
        String jString = "alert('122')";
        ((JavascriptExecutor) driver).executeScript(jString);

        //获取单个元素
        WebElement element = driver.findElement(By.id("wrapper"));
        System.out.println(element.getAttribute("class"));

        //获取多个元素
        List<WebElement> elList = driver.findElements(By.tagName("input"));
        for (WebElement e : elList) {
            System.out.println("获取多个元素:"+e.getAttribute("name"));
        }

        //定位层级元素
        WebElement e = driver.findElement(By.cssSelector("#qrcode-item qrcode-item-1"));
        List<WebElement> list = e.findElements(By.tagName("div"));
        for (WebElement e1 : list) {
            System.out.println("定位层级元素:"+e1.getAttribute("class"));
        }



        //获取当前的窗口
        String currentWindow = driver.getWindowHandle();

        //获取所有的窗口
        Set<String> handles = driver.getWindowHandles();

        //遍历窗口
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()) {
            if (currentWindow == iterator.next())
                continue;
            //跳转到弹出的窗口
            WebDriver driver2 = driver.switchTo().window(iterator.next());
            driver2.getTitle();

        }


        //处理弹出框
        Alert alert = driver.switchTo().alert();
        alert.getText();
        alert.dismiss();//相当于点击取消


        Alert confirm = driver.switchTo().alert();
        confirm.getText();
        confirm.accept();//相当于点击确认


        Alert prompt = driver.switchTo().alert();
        prompt.getText();
        prompt.accept();
        prompt.sendKeys("测试1");//输入值

        //处理下拉列表
        Select select = new Select(driver.findElement(By.id("select")));
        select.selectByIndex(1);
        select.selectByValue("西安");
        select.selectByVisibleText("咸阳");
        //获取下拉框的全部选项
        List<WebElement> list2 = select.getOptions();
        for (WebElement webElement : list2) {
            webElement.click();//点击下拉框
        }

        //处理cookie
        //添加一个cookie
        Cookie cookie = new Cookie("COOKIE", "NAME","D://COOKIES");
        driver.manage().addCookie(cookie);

        //获取cookies
        Set<Cookie> set = driver.manage().getCookies();
        Iterator<Cookie> iterator2 = set.iterator();
        while (iterator2.hasNext()) {
            Cookie c = iterator2.next();
            c.getName();
            c.getValue();
            c.getPath();

        }

        driver.manage().deleteAllCookies();
        driver.manage().deleteCookie(cookie);
        driver.manage().deleteCookieNamed("COOKIE");


        //等待加载完页面
        try {
            driver.manage().timeouts().wait(1);
            driver.manage().timeouts().implicitlyWait(1,TimeUnit.SECONDS);//等待界面加载完
        } catch (InterruptedException e2) {

            e2.printStackTrace();
        }
        driver.close();
    }
}
